﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace ExportFethionHistory
{
    public partial class Form1 : Form
    {
        CConfig config = new CConfig("f2g.xml");
        String preInitialDirectory=@"c:\";
        public Form1()
        {
            InitializeComponent();
            ImportSetting();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void ImportSetting()
        {
            textBox_FethionDataFile.Text = config.Get("FethionDataFile");
            textBox_FethionID.Text = config.Get("FethionID");
            textBox_GmailName.Text = config.Get("GmailName");
            bool isChecked;
            if (bool.TryParse(config.Get("SavePwd"), out isChecked))
            {
                checkBox_Savepwd.Checked = isChecked;
                textBox_GmailPass.Text = config.Get("GmailPass");
            }
        }

        private void SaveSetting()
        {
            config.Set("FethionDataFile",textBox_FethionDataFile.Text );
            config.Set("FethionID",textBox_FethionID.Text) ;
            config.Set("GmailName", textBox_GmailName.Text);
            if(checkBox_Savepwd.Checked)
                config.Set("SavePwd",bool.TrueString );
            else
                config.Set("SavePwd", bool.FalseString);
            config.Set("GmailPass", "");
            if (checkBox_Savepwd.Checked)
                config.Set("GmailPass", textBox_GmailPass.Text);
            config.Save();
        }

        private void button_Send_Click(object sender, EventArgs e)
        {
            SaveSetting();
            button_Send.Enabled = false;

            ExportFethionHistory instance = new ExportFethionHistory(this,this.checkBox_Test.Checked);
            instance.StartExport(textBox_GmailName.Text, textBox_GmailPass.Text, textBox_FethionDataFile.Text, textBox_FethionID.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter =
               "dat files (history.dat)|history.dat";
            dialog.InitialDirectory = preInitialDirectory;
            dialog.Title = "Select a text file";
            String dstName= (dialog.ShowDialog() == DialogResult.OK)
               ? dialog.FileName : null;
            textBox_FethionDataFile.Text = dstName;
 
        }

        public void SetControlPropertyValue(int propValue)
        {
            this.progressBar.Value = propValue;
        }
        public void SetEnable(bool isRunning)
        {
            textBox_FethionDataFile.Enabled = isRunning;
            button1.Enabled = isRunning;
            textBox_FethionDataFile.Enabled = isRunning;
            textBox_FethionID.Enabled = isRunning;
            textBox_GmailName.Enabled = isRunning;
            textBox_GmailPass.Enabled = isRunning;
            button_Send.Enabled = isRunning;
            progressBar.Enabled = !isRunning;
            checkBox_Test.Enabled = isRunning;
        }
        public void AddLog(String log)
        {
            textBox_Log.Text += log+"\r\n";
            textBox_Log.SelectionStart = textBox_Log.Text.Length;
            textBox_Log.ScrollToCaret();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            SaveSetting();
        }

    }
}
